<?php

namespace Admin\Controller;


class AuthSetController extends CommonController
{

    function __construct()
    {
        parent::__construct();
        //查出所有用户组
        $this->Auth_group = M('Auth_group');
        $this->groups = $this->Auth_group->select();
    }

    /***
     * 加载用户管理首页
     */
    public function user_manage()
    {
        $User = D("User");
        $users = $User->relation(true)->select();
        $count = $User->count();
//        p($users);exit;
        $this->assign(compact('users', 'count'));
        $this->display();
    }

    /***
     * 新增用户管理
     */
    public function add_user()
    {
        $User = M('User');
        $AuthGroupAccess = M('AuthGroupAccess');
        /***
         * 注：添加用户往用户表(user)和用户明细表(auth_group_access)同时插入数据
         */
//        if (IS_POST) {
////            dump($_POST);exit;
//            $AuthGroupAccess = M('AuthGroupAccess');
//            $User = D("User"); // 实例化User对象
//            if (!$User->create()) {
//                // 如果创建失败 表示验证没有通过 输出错误提示信息
//                exit($User->getError());
//            } else {
//                // 验证通过 可以进行其他数据操作
//                $User->create();
//                $User->password = set_password(I('post.password'));
//                $User->token = make_token() . time();
//                $User->created_at = time();
//
//                $uid = $User->add();
//                $group_ids = I('post.group_id');
//
//                foreach ($group_ids as $v) {
//                    $data['uid'] = $uid;
//                    $data['group_id'] = $v;
//                    $AuthGroupAccess->add($data);
//                }
//
//                $this->success('新增成功~', U('user_manage'));
//            }
//        }
//        else {
//            $this->display();
//        }

        if (IS_AJAX) {
            $data['username'] = trim(I('post.username'));
            $data['mobile'] = I('post.mobile');
            $data['email'] = I('post.email');
            $data['password'] = set_password(I('post.password'));
            $check_password = I('post.check_password');
            $group_ids = I('post.group_id');

            $user = $User->where("username = '$data[username]'")->find();
            $email = $User->where("email = '$data[email]'")->find();
            $mobile = $User->where("mobile = '$data[mobile]'")->find();

            $patter = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/";
            $telephone = "/^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$/";

            $file = array('status' => 1, 'msg' => '添加用户成功');

            if ($user) {
                $file = array('status' => 0, 'msg' => '用户名已存在');
            }
            if ($email) {
                $file = array('status' => 0, 'msg' => '邮箱已存在');
            }
            if ($mobile) {
                $file = array('status' => 0, 'msg' => '手机号码已存在');
            }
            if (!preg_match($patter, $data['email'])) {
                $file = array('status' => 0, 'msg' => '邮箱格式不正确');
            }
            if (!preg_match($telephone, $data['mobile'])) {
                $file = array('status' => 0, 'msg' => '手机格式不正确');
            }
            if (strlen($data['username']) < 3) {
                $file = array('status' => 0, 'msg' => '用户名长度不能小于三个长度');
            }
            if (strlen(I('post.password')) < 6) {
                $file = array('status' => 0, 'msg' => '密码长度不能小于6位');
            }
            if ($check_password != I('post.password')) {
                $file = array('status' => 0, 'msg' => '两次密码输入不一致');
            }
            if ($group_ids == "") {
                $file = array('status' => 0, 'msg' => '用户组不能为空');
            }
            if ($file['status'] == 1) {
                $data['created_at'] = time();
                $data['token'] = make_token() . time();
                $uid = $User->add($data);

                foreach ($group_ids as $v) {
                    $face['uid'] = $uid;
                    $face['group_id'] = $v;
                    $AuthGroupAccess->add($face);
                }
            }
            $this->ajaxReturn($file);
        } else {
            $this->display();
        }
    }

    /***
     * 编辑用户
     */
    public function edit_user()
    {
        $id = I('get.id');
        $User = D("User");
        $AuthGroupAccess = M('AuthGroupAccess');
        $user = $User->relation(true)->find($id);
        $group_id = pluck($user['groups'], 'id');
        if (IS_AJAX) {
//            p($_POST);exit;
            $data['username'] = trim(I('post.username'));
            $data['mobile'] = I('post.mobile');
            $data['email'] = I('post.email');
            $data['password'] = set_password(I('post.password'));
            $check_password = I('post.check_password');
            $group_ids = I('post.group_id');
            $id = I('post.id');
//            $group_ids = I('post.group_id');

            $user = $User->where("username = '$data[username]'and id !='$id'")->find();
            $email = $User->where("email = '$data[email]'and id !='$id'")->find();
            $mobile = $User->where("mobile = '$data[mobile]'and id !='$id'")->find();

            $patter = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/";
            $telephone = "/^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$/";

            $file = array('status' => 1, 'msg' => '编辑用户成功');

            if ($user) {
                $file = array('status' => 0, 'msg' => '用户名已存在');
            }
            if ($email) {
                $file = array('status' => 0, 'msg' => '邮箱已存在');
            }
            if ($mobile) {
                $file = array('status' => 0, 'msg' => '手机号码已存在');
            }
            if (!preg_match($patter, $data['email'])) {
                $file = array('status' => 0, 'msg' => '邮箱格式不正确');
            }
            if (!preg_match($telephone, $data['mobile'])) {
                $file = array('status' => 0, 'msg' => '手机格式不正确');
            }
            if (strlen($data['username']) < 3) {
                $file = array('status' => 0, 'msg' => '用户名长度不能小于三个长度');
            }
            if (strlen(I('post.password')) < 6) {
                $file = array('status' => 0, 'msg' => '密码长度不能小于6位');
            }
            if ($check_password != I('post.password')) {
                $file = array('status' => 0, 'msg' => '两次密码输入不一致');
            }
            if ($group_ids == "") {
                $file = array('status' => 0, 'msg' => '用户组不能为空');
            }
            if ($file['status'] == 1) {
                $data['created_at'] = time();
                $data['token'] = make_token() . time();
                $User->where("id = '$id'")->save($data);

                //编辑用户组先删除原来用户所在的组，重新增加
                $AuthGroupAccess->where("uid = '$id'")->delete();
                foreach ($group_ids as $v) {
                    $face['uid'] = $id;
                    $face['group_id'] = $v;
                    $AuthGroupAccess->add($face);
                }
            }
            $this->ajaxReturn($file);
        }
//        if (IS_POST) {
//            $User = D("User"); // 实例化User对象
//            if (!$User->create()) {
//                // 如果创建失败 表示验证没有通过 输出错误提示信息
//                exit($User->getError());
//            } else {
//                // 验证通过 可以进行其他数据操作
//                $User->create();
//                $User->password = set_password(I('post.password'));
//                $User->token = make_token() . time();
//                $User->created_at = time();
//                $User->where("id = '$id'")->save();
//
        //编辑用户组先删除原来用户所在的组，重新增加
//                $group_ids = I('post.group_id');
//                $AuthGroupAccess->where("uid = '$id'")->delete();
//                foreach ($group_ids as $v) {
//                    $face['uid'] = $id;
//                    $face['group_id'] = $v;
//                    $AuthGroupAccess->add($face);
//                }
//                $this->success('编辑成功', U('user_manage'));
//            }
//        }
        else {
            $this->assign(compact('user', 'group_id'));
            $this->display();
        }
    }

    /***
     * 删除用户
     */
    public function delete()
    {
        $id = I('post.id');
        $User = D('User');
        $AuthGroupAccess = M('AuthGroupAccess');
        $AuthGroupAccess->where("uid = '$id'")->delete();
        $User->where("id = '$id'")->delete();
    }

    /***
     * 改变属性
     */
    public function change_attr()
    {
        $User = M('User');

        if (IS_AJAX) {
        $id = I('post.id');
        $attr = !I('post.attr');
        $User->where("id = '$id'")->setField('status',$attr);
        }
    }

    /***
     * 加载用户组管理首页
     */
    public function group_manage()
    {
//        $AuthRule = M('AuthRule');
//        //一级菜单
//        $rules = $AuthRule->where("parent_id = 0")->select();
//        //二级菜单
//        foreach ($rules as $key => $value) {
//            $children = $AuthRule->where("parent_id = '$value[id]'")->select();
//            $rules[$key]['children'] = $children;

//            //三级菜单
//            foreach ($rules[$key]['children'] as $k =>$v){
//                $child = $AuthRule->where("parent_id = '$v[id]'")->select();
//                $rules[$key]['children'][$k]['child'] = $child;
//            }
//        }

//        p($rules);exit;
        $AuthGroup = M('AuthGroup');
        $count = $AuthGroup->count();
        $this->assign(compact('count'));
        $this->display();
    }

    /***
     * 新增用户组
     */
    public function add_group()
    {
        $AuthGroup = M('AuthGroup');
        if (IS_AJAX) {
            $data['title'] = I('post.title');
            $data['rules'] = I('post.rules');
            $data['created_at'] = time();
            $authgroup = $AuthGroup->where("title = '$data[title]'")->find();
            $face = array('status' => 1, 'msg' => '新增成功');


            if ($data['title'] == "") {
                $face = array('status' => 0, 'msg' => '用户组名不能为空');
            }
            if ($authgroup) {
                $face = array('status' => 0, 'msg' => '用户组名已经存在');
            }
            if ($data['rules'] == "") {
                $face = array('status' => 0, 'msg' => '权限选择不能为空');
            }
            if ($face['status'] == 1) {
                $data['rules'] = implode(',', $data['rules']);
                $AuthGroup->add($data);
            }
            $this->ajaxReturn($face);
        } else {
            $this->display();
        }

    }

    /***
     * 编辑用户组
     */
    public function edit_group()
    {
        $AuthGroup = M('AuthGroup');
        $id = I('get.id');

//        if (IS_POST) {
//            $data['title'] = trim(I('post.title'));
//            $data['rules'] = implode(',', I('post.rule_id'));
//            $AuthGroup->where("id = '$id'")->save($data);
//            $this->success('编辑用户组成功', U('group_manage'));
//        }
        if (IS_AJAX) {
//            p($_POST);exit;
            $data['title'] = I('post.title');
            $data['rules'] = implode(',', I('post.rule_id'));
            $data['created_at'] = time();
            $data['status'] = 1;
            $id = I('post.id');
            $authgroup = $AuthGroup->where("title = '$data[title]'and id !='$id'")->find();

            $face = array('status' => 1, 'msg' => '编辑成功');

            if ($data['title'] == "") {
                $face = array('status' => 0, 'msg' => '用户组名不能为空');
            }
            if ($authgroup) {
                $face = array('status' => 0, 'msg' => '用户组名已经存在');
            }
            if ($data['rules'] == "") {
                $face = array('status' => 0, 'msg' => '权限选择不能为空');
            }
            if ($face['status'] == 1) {
                $AuthGroup->where("id = '$id'")->save($data);
            }
            $this->ajaxReturn($face);
        }
        else {
            $group = $AuthGroup->where("id = '$id'")->find();
            $this->assign(compact('group'));
            $this->display();
        }
    }

    /***
     * 删除用户组
     */
    public function delete_group()
    {
        $id = I('post.id');
        $AuthGroup = M('AuthGroup');
        $AuthGroup->where("id = '$id'")->delete();
    }

    /***
     * 加载权限首页
     */
    public function rule_manage()
    {
        $this->display();
    }

    /***
     * 新增用户权限
     */
    public function add_rule()
    {
        $AuthRule = M('AuthRule');
        if (IS_AJAX) {
//            p($_POST);exit;
            $data['parent_id'] = I('post.parent_id');
            $data['icon'] = I('post.icon');
            $data['title'] = I('post.title');
            $data['name'] = I('post.name');
//            $icon = $AuthRule->where("icon='$data[icon]'")->find();
            $name = $AuthRule->where("name='$data[name]'")->find();
            $title = $AuthRule->where("title = '$data[title]'")->find();
            $face = array('status' => 1, 'msg' => '新增成功');

//            if ($icon) {
//                $face = array('status' => 0, 'msg' => '图标已存在');
//            }
            if ($name) {
                $face = array('status' => 0, 'msg' => '权限名称已存在');
            }
//            if ($title) {
//                $face = array('status' => 0, 'msg' => '菜单名称已存在');
//            }
            if (I('post.name') == "") {
                $face = array('status' => 0, 'msg' => '权限名称不能为空');
            }
            if (I('post.title') == "") {
                $face = array('status' => 0, 'msg' => '菜单名称不能为空');
            }
            if ($face['status'] == 1) {
                $AuthRule->add($data);
            }
            $this->ajaxReturn($face);
        }
    }

    /***
     * 删除主菜单
     */
    public function delete_rule()
    {
        $AuthRule = M('AuthRule');

        if (IS_AJAX) {
            $id = I('post.id');
            //判断主菜单下是否有二级菜单，有二级菜单不能删除
            $count = $AuthRule->where("parent_id='$id'")->find();
            if ($count > 0) {
                $info = array('status' => 0, 'msg' => '此菜单下有二级菜单不能删除');
            } else {
                $AuthRule->where("id = '$id'")->delete();
                $info = array('status' => 1, 'msg' => '删除成功');
            }
            $this->ajaxReturn($info);
        }

    }

    /***
     * 编辑菜单权限
     */
    public function edit_rule()
    {
        $AuthRule = M('AuthRule');
        $id = I('get.id');

        if (IS_AJAX) {
            $data['parent_id'] = I('post.parent_id');
            $data['icon'] = I('post.icon');
            $data['title'] = I('post.title');
            $data['name'] = I('post.name');
            $id = I('post.id');
            $icon = $AuthRule->where("icon='$data[icon]'and id!='$id'")->find();
            $name = $AuthRule->where("name='$data[name]'and id!='$id'")->find();
            $title = $AuthRule->where("title = '$data[title]'and id!='$id'")->find();
            $face = array('status' => 1, 'msg' => '新增成功');

            if ($icon) {
                $face = array('status' => 0, 'msg' => '图标已存在');
            }
            if ($name) {
                $face = array('status' => 0, 'msg' => '权限名称已存在');
            }
            if ($title) {
                $face = array('status' => 0, 'msg' => '菜单名称已存在');
            }
            if ($data['name'] == "") {
                $face = array('status' => 0, 'msg' => '权限名称不能为空');
            }
            if ($data['title'] == "") {
                $face = array('status' => 0, 'msg' => '菜单名称不能为空');

            }
            if ($face['status'] == 1) {
                $AuthRule->where("id = '$id'")->save($data);
            }
            $this->ajaxReturn($face);

        } else {
            $rul = $AuthRule->where("id = '$id'")->find();
            $this->assign(compact('rul'));
            $this->display();
        }
    }

}